package com.hiwan.unions.activity.membership;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.adapter.bean.MemData;
import com.hiwan.unions.adapter.bean.MembersAdresssBean;
import com.hiwan.unions.adapter.bean.MembersBean;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.ACache;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.EditUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.hiwan.unions.util.Utils;
import com.hiwan.unions.view.popupWindow.DatePop;
import com.hiwan.unions.view.popupWindow.MembersListPop;
import com.hiwan.unions.view.popupWindow.MembershipPop;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/7/8.
 */
public class MembershipActivity extends RootActivity implements MembershipPop.OnTextLinstener, DatePop.OnTextLinstener, MembersListPop.OnTextListLinstener, HttpListener<JSONObject> {
    private EditText edit_name, edit_card, edit_phone, edit_userAddress, edit_gz, edit_zw, edit_address_4;
    private TextView edit_sex, edit_wh, edit_zz, edit_mz, edit_cardDate, edit_address_1, edit_address_2, edit_address_3;
    private MembershipPop membershipPop;
    private DatePop datePop;
    JSONArray jsonArray;
    private ACache mACache;
    private ArrayList<String> arry_years = new ArrayList<String>();
    private ArrayList<MembersBean> arry_mz = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_xb = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_wh = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_zz = new ArrayList<MembersBean>();

    private ArrayList<MembersAdresssBean> arry_years1 = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_years2 = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_years3 = new ArrayList<MembersAdresssBean>();
    private int i = 0;
    private String code0, code1, code2, code3, code4 = "", code4_1 = "", code5 = "", code6 = "";
    Request<JSONObject> request;
    private String member_name, member_sex, member_nation = "汉族", member_culture, member_politicalStatus, member_card_id, member_card_id_date, member_household_addr, member_company, member_job, member_company_addr1 = "", member_company_addr1_2 = "", member_company_addr2, member_company_addr3, member_company_addr4;
    private MembersListPop listPop;
    MembersAdresssBean bean;
    private View view;
    private MemData memData;

    public static void startPreview(Context context, String real, String title, boolean type) {
        Intent intent = new Intent(context, MembershipActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("real", real);
        bundle.putString("title", title);
        bundle.putBoolean("type", type);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_membershi;
    }

    @Override
    protected void initData() {
        stringArrayList(Utils.getJson(this, "minzu.txt"), arry_mz);
        stringArrayList(Utils.getJson(this, "sex.txt"), arry_xb);
        stringArrayList(Utils.getJson(this, "wenhua.txt"), arry_wh);
        stringArrayList(Utils.getJson(this, "zz.txt"), arry_zz);
        bundle = getIntent().getExtras();
        setTitleTxt(bundle.getString("title"));
        edit_card.setText(bundle.getString("real"));
        edit_phone.setText(userInfo.getString(ConstantsValues.phone, ""));
        request = NoHttp.createJsonObjectRequest(ConstsNet.getAreaList, RequestMethod.POST);
        request.add("levelType", "1");
        CallServer.getRequestInstance().addToken(this, 2, request, this, true, userInfo);
        mACache = ACache.get(this);
        memData = (MemData) mACache.getAsObject(userInfo.getString(ConstantsValues.uuid, ""));
        getDate(memData);
        if (bundle.getBoolean("type")) {
            request = NoHttp.createJsonObjectRequest(ConstsNet.getMemberInfo, RequestMethod.POST);
            request.add("uuid", userInfo.getString(ConstantsValues.uuid, ""));
            CallServer.getRequestInstance().addToken(this, 1, request, this, true, userInfo);
        }
    }

    private void getDate(MemData memData) {

        if (memData != null) {
            edit_name.setText(memData.getName());
            if (!memData.getMember_nation().equals(""))
                member_nation = memData.getMember_nation();
            member_sex = memData.getSex();
            member_culture = memData.getMember_culture();
            member_politicalStatus = memData.getMember_politicalStatus();
            code0 = fo(arry_xb, member_sex, edit_sex);
            code2 = fo(arry_wh, member_culture, edit_wh);
            code3 = fo(arry_zz, member_politicalStatus, edit_zz);
            edit_cardDate.setText(memData.getReal_date());
            edit_userAddress.setText(memData.getName_address());
            edit_gz.setText(memData.getGz());
            edit_zw.setText(memData.getZw());
            code4_1 = memData.getGz_address_1Code();
            code4 = memData.getGz_address_1_2Code();
            member_company_addr1 = memData.getGz_address_1();
            member_company_addr1_2 = memData.getGz_address_1_2();
            edit_address_1.setText(member_company_addr1 + member_company_addr1_2);
            code5 = memData.getGz_address_2Code();
            edit_address_2.setText(memData.getGz_address_2());
            code6 = memData.getGz_address_3Code();
            edit_address_3.setText(memData.getGz_address_3());
            edit_address_4.setText(memData.getGz_address_4());
        }
        code1 = fo(arry_mz, member_nation, edit_mz);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!CheckUtils.isEmpty(userInfo.getString(ConstantsValues.uuid, ""))) {
            if (memData == null)
                memData = new MemData();
            memData.setName(edit_name.getText().toString());
            memData.setSex(edit_sex.getText().toString());
            memData.setMember_nation(edit_mz.getText().toString());
            memData.setMember_culture(edit_wh.getText().toString());
            memData.setMember_politicalStatus(edit_zz.getText().toString());
            memData.setReal_date(edit_cardDate.getText().toString());
            memData.setName_address(edit_userAddress.getText().toString());
            memData.setGz(edit_gz.getText().toString());
            memData.setZw(edit_zw.getText().toString());
            memData.setGz_address_1(member_company_addr1);
            memData.setGz_address_1Code(code4_1);
            memData.setGz_address_1_2(member_company_addr1_2);
            memData.setGz_address_1_2Code(code4);
            memData.setGz_address_2(edit_address_2.getText().toString());
            memData.setGz_address_2Code(code5);
            memData.setGz_address_3(edit_address_3.getText().toString());
            memData.setGz_address_3Code(code6);
            memData.setGz_address_4(edit_address_4.getText().toString());
            mACache = ACache.get(this);
            mACache.put(userInfo.getString(ConstantsValues.uuid, ""), memData);
            Logger.i("执行？？？啊啊啊啊啊啊啊啊啊啊啊啊");
        }
    }

    private ArrayList<String> stringArrayList(ArrayList<MembersBean> strings) {
        arry_years.clear();
        for (MembersBean s : strings) {
            arry_years.add(s.getTitle());
        }
        return arry_years;
    }

    private ArrayList<String> stringArrayList2(ArrayList<MembersAdresssBean> strings) {
        arry_years.clear();
        for (MembersAdresssBean s : strings) {
            arry_years.add(s.getArea_name());
        }
        return arry_years;
    }

    private String fo2(ArrayList<MembersBean> list, String id, TextView textView) {
        for (int i = 0; i < list.size(); i++) {
            MembersBean bean = list.get(i);
            if (bean.getId().equals(id)) {
                textView.setText(bean.getTitle());
                return bean.getId();
            }
        }
        return "";
    }

    private String fo(ArrayList<MembersBean> list, String title, TextView textView) {
        for (int i = 0; i < list.size(); i++) {
            MembersBean bean = list.get(i);
            if (bean.getTitle().equals(title)) {
                textView.setText(bean.getTitle());
                return bean.getId();
            }
        }
        return "";
    }


    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_name = findView(R.id.edit_name);
        edit_mz = findView(R.id.edit_mz);
        edit_card = findView(R.id.edit_card);
        edit_phone = findView(R.id.edit_phone);
        EditUtils.phoneNumAddSpace(edit_phone);
        edit_userAddress = findView(R.id.edit_userAddress);
        edit_gz = findView(R.id.edit_gz);
        edit_zw = findView(R.id.edit_zw);
        edit_sex = findView(R.id.edit_sex);
        edit_wh = findView(R.id.edit_wh);
        edit_zz = findView(R.id.edit_zz);
        edit_address_1 = findView(R.id.edit_address_1);
        edit_address_2 = findView(R.id.edit_address_2);
        edit_address_3 = findView(R.id.edit_address_3);
        edit_address_4 = findView(R.id.edit_address_4);
        edit_cardDate = findView(R.id.edit_cardDate);
        membershipPop = new MembershipPop(this);
        membershipPop.setOnTextLinstener(this);
        datePop = new DatePop(this);
        datePop.setOnTextLinstener(this);
        setOperationBtn("提交");
    }


    @Override
    public void OnText(String text, int z) {
        if (i == 0) {
            code0 = arry_xb.get(z).getId();
            edit_sex.setText(text);
        } else if (i == 1) {
            code1 = arry_mz.get(z).getId();
            edit_mz.setText(text);
        } else if (i == 2) {
            code2 = arry_wh.get(z).getId();
            edit_wh.setText(text);
        } else if (i == 3) {
            code3 = arry_zz.get(z).getId();
            edit_zz.setText(text);
        } else if (i == 4) {
            code5 = arry_years2.get(z).getArea_code();
            code6 = "";
            edit_address_3.setText("");
            if (text.equals("不确定")) {
                code6 = code5;
                edit_address_3.setText("不确定");
            }
            edit_address_2.setText(text);
        } else if (i == 5) {
            Logger.i(" arry_years3.get(z).getArea_code()" + arry_years3.get(z).getArea_code());
            code6 = arry_years3.get(z).getArea_code();
            edit_address_3.setText(text);
        }
    }

    @Override
    public void OnDateText(int curYear, String curMonth, String curDate) {
        edit_cardDate.setText(curYear + "-" + curMonth + "-" + curDate);
    }

    @Override
    public void OnDateText(String id, String text, String id2, String text2) {
        Logger.i("text   " + text + text2);
        member_company_addr1 = text;
        member_company_addr1_2 = text2;
        String addre = member_company_addr1 + member_company_addr1_2;
        if (addre.equals(edit_address_1.getText().toString())) {
            return;
        }
        code4_1 = id;
        code4 = id2;
        edit_address_1.setText(text + text2);
        edit_address_2.setText("");
        edit_address_3.setText("");
    }

    private void setList(String parent_area_code, int i) {
        if (CheckUtils.isEmpty(parent_area_code)) {
            String s;
            if (i == 3) {
                s = "请选择地市/区县";
            } else {
                s = "请选择街道";
            }
            ToastUtils.showToast(this, s);
            return;
        }
        if (i == 5) {
            if (CheckUtils.checkBoolean(edit_address_3, "不确定")) {
                return;
            }
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.getAreaList, RequestMethod.POST);
        request.add("levelType", "2");
        request.add("parent_area_code", parent_area_code);
        CallServer.getRequestInstance().addToken(this, i, request, this, true, userInfo);
    }

    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        try {
            member_name = CheckUtils.check(edit_name, getString(R.string.enter_userName_error2));
            member_sex = CheckUtils.check(edit_sex, getString(R.string.enter_sex_error2));
            member_nation = CheckUtils.check(edit_mz, getString(R.string.enter_nation_error2));
            member_culture = CheckUtils.check(edit_wh, getString(R.string.enter_culture_error2));
            member_politicalStatus = CheckUtils.check(edit_zz, getString(R.string.enter_politicalStatus_error2));
            member_card_id = CheckUtils.checkReal(edit_card);
            member_card_id_date = CheckUtils.check(edit_cardDate, getString(R.string.enter_realDate_error2));
            member_household_addr = CheckUtils.check(edit_userAddress, getString(R.string.enter_household_error2));
            member_company = CheckUtils.check(edit_gz, getString(R.string.enter_company_error2));
            member_job = CheckUtils.check(edit_zw, getString(R.string.enter_job_error2));
            CheckUtils.check(edit_address_1, getString(R.string.enter_address1_error2));
            member_company_addr2 = CheckUtils.check(edit_address_2, getString(R.string.enter_address2_error2));
            member_company_addr3 = CheckUtils.check(edit_address_3, getString(R.string.enter_address3_error2));
            member_company_addr4 = CheckUtils.check(edit_address_4, getString(R.string.enter_address4_error2));
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.insertMember, RequestMethod.POST);
        request.add("member_name", member_name);
        request.add("member_sex", code0);
        request.add("member_nation", code1);
        request.add("member_culture", code2);
        request.add("member_politicalStatus", code3);
        request.add("member_card_id", member_card_id);
        request.add("member_card_id_date", member_card_id_date);
        request.add("member_household_addr", member_household_addr);
        request.add("member_company", member_company);
        request.add("member_job", member_job);
        request.add("member_company_city", code4_1);
        request.add("member_company_district", code4);
        request.add("member_company_street", code5);
        request.add("member_company_community", code6);
        request.add("member_company_addr", member_company_addr4);
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);

    }

    private void stringArrayList(String s, ArrayList<MembersBean> strings) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                MembersBean bean = new MembersBean();
                bean.setId(JsonUtil.getString(jsonObject, "id"));
                bean.setTitle(JsonUtil.getString(jsonObject, "title"));
                strings.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void OnClicks(View view, ArrayList<String> list, String content) {
        if (list.size() == 0) {
            ToastUtils.showToast(this, "暂无数据选择");
            return;
        }
        membershipPop.showPopupWindow(view, list, content);

    }

    public void ButtonOnClick(View view) {
        switch (view.getId()) {
            case R.id.sex_layout:
                i = 0;
                OnClicks(view, stringArrayList(arry_xb), edit_sex.getText().toString());
                break;
            case R.id.mz_layout:
                i = 1;
                OnClicks(view, stringArrayList(arry_mz), edit_mz.getText().toString());
                break;
            case R.id.wh_layout:
                i = 2;
                OnClicks(view, stringArrayList(arry_wh), edit_wh.getText().toString());
                break;
            case R.id.zz_layout:
                i = 3;
                OnClicks(view, stringArrayList(arry_zz), edit_zz.getText().toString());
                break;
            case R.id.cardDate_layout:
                datePop.showPopupWindow(view, edit_cardDate.getText().toString());
                break;
            case R.id.layout_address_1:
                if (listPop == null) {
                    listPop = new MembersListPop(this, arry_years1);
                    listPop.setOnTextLinstener(this);
                }
                listPop.showPopupWindow(view, code4_1, code4);
                break;
            case R.id.layout_address_2:
                this.view = view;
                i = 4;
                setList(code4, 3);
                break;
            case R.id.layout_address_3:
                this.view = view;
                i = 5;
                setList(code5, 4);
                break;

        }
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        switch (what) {
            case 0:
                ToastUtils.showToast(this, "已提交，我们会尽快处理");
                MainActivity.startPreview(this);
                finish();
                break;
            case 1:
                jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
                member_name = JsonUtil.getString(jsonObject, "member_name");
                edit_name.setText(member_name);
                code0 = JsonUtil.getString(jsonObject, "member_sex");
                fo2(arry_xb, code0, edit_sex);
                code1 = JsonUtil.getString(jsonObject, "member_nation");
                fo2(arry_mz, code1, edit_mz);
                code2 = JsonUtil.getString(jsonObject, "member_culture");
                fo2(arry_wh, code2, edit_wh);
                code3 = JsonUtil.getString(jsonObject, "member_politicalStatus");
                fo2(arry_zz, code3, edit_zz);
                member_card_id = JsonUtil.getString(jsonObject, "member_card_id");
                edit_card.setText(member_card_id);
                member_card_id_date = JsonUtil.getString(jsonObject, "member_card_id_date");
                edit_cardDate.setText(member_card_id_date);
                edit_phone.setText(userInfo.getString(ConstantsValues.phone, ""));
                member_household_addr = JsonUtil.getString(jsonObject, "member_household_addr");
                edit_userAddress.setText(member_household_addr);
                member_company = JsonUtil.getString(jsonObject, "member_company");
                edit_gz.setText(member_company);
                member_job = JsonUtil.getString(jsonObject, "member_job");
                edit_zw.setText(member_job);
                code4_1 = JsonUtil.getString(jsonObject, "company_city");
                code4 = JsonUtil.getString(jsonObject, "company_district");
                code5 = CheckUtils.isEmpty(JsonUtil.getString(jsonObject, "company_street"), "000000");
                code6 = CheckUtils.isEmpty(JsonUtil.getString(jsonObject, "company_community"), "000000");
                edit_address_1.setText(JsonUtil.getString(jsonObject, "company_city_name") + JsonUtil.getString(jsonObject, "company_district_name"));
                edit_address_2.setText(CheckUtils.isEmpty(JsonUtil.getString(jsonObject, "company_street_name"), "不确定"));
                edit_address_3.setText(CheckUtils.isEmpty(JsonUtil.getString(jsonObject, "company_community_name"), "不确定"));
                edit_address_4.setText(JsonUtil.getString(jsonObject, "member_company_addr"));
                break;
            case 2:
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years1.add(bean);
                }
                break;
            case 3:
                arry_years2.clear();
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years2.add(bean);
                }
                bean = new MembersAdresssBean();
                bean.setArea_code("000000");
                bean.setArea_name("不确定");
                bean.setLevel("2");
                bean.setParent_area_code("000000");
                arry_years2.add(bean);
                OnClicks(view, stringArrayList2(arry_years2), edit_address_2.getText().toString());
                break;
            case 4:
                arry_years3.clear();
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    MembersAdresssBean bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years3.add(bean);
                }
                bean = new MembersAdresssBean();
                bean.setArea_code("000000");
                bean.setArea_name("不确定");
                bean.setLevel("2");
                bean.setParent_area_code("000000");
                arry_years3.add(bean);
                OnClicks(view, stringArrayList2(arry_years3), edit_address_3.getText().toString());
                break;
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        ToastUtils.showToast(this, networkMillis);
        switch (errCode) {
            case 201:
                MainActivity.startPreview(this, com.hiwan.unions.activity.system.MembershipActivity.class, "MyFragment");
                break;
        }

    }
}
